from functools import partial
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from pages_info import *

# 工具栏信息列表（标题, 页面索引）
toolbar_info = ["21MM", "24MM", "26MM:Step3:CECHAL", "Nissan"]
toolbar_dict = {
    "21MM":0,
    "24MM":1,
    "Step3":2,
    "CECHAL":3,
    "Nissan":4
}
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("check release node")
        self.setGeometry(500, 300, 600, 300)
        # 创建堆叠窗口用于管理不同的页面
        self.stack = QStackedWidget()
        self.setCentralWidget(self.stack)
        # 页面内容列表
        page_contents = [
            Page_21MM(),
            Page_24MM(),
            Page_26MM_Step3(),
            Page_26MM_CECHAL(),
            Page_Nissan()
        ]
        # 创建页面
        self.create_pages(page_contents)
        # 创建并添加工具栏
        self.create_toolbars()
    
    def create_pages(self,page_contents):
        for content in page_contents:
            self.stack.addWidget(content)
    
    def create_toolbars(self):
        toolbar = self.addToolBar("Main Toolbar")
        for titles in toolbar_info:
            if ":" in titles:
                # 创建下拉列表（子工具栏）
                combo = QComboBox()
                # combo.addItem("请选择操作", -1)  # 添加默认选项
                for i, title in enumerate(titles.split(':')):
                    combo.addItem(title, i)
                # 连接下拉列表的选择事件到切换页面的槽函数
                combo.currentIndexChanged.connect(self.on_combobox_changed)
                toolbar.addWidget(combo)
            else:
                action = QAction(f"{titles}", self)
                # action.triggered.connect(partial(self.stack.setCurrentIndex, toolbar_dict[titles]))
                action.triggered.connect(partial(self.switch_page, toolbar_dict[titles]))
                toolbar.addAction(action)
    
    #定义下拉框选择变化时的槽函数
    def on_combobox_changed(self):
        sender = self.sender()
        if isinstance(sender, QComboBox):
            current_text = sender.currentText()
            if current_text in toolbar_dict:
                # self.stack.setCurrentIndex(toolbar_dict[current_text])
                self.switch_page(toolbar_dict[current_text])

    def switch_page(self, index):
        """切换页面并重置页面状态"""
        # 获取当前页面并重置
        current_widget = self.stack.currentWidget()
        if hasattr(current_widget, 'clear'):
            current_widget.clear()
        
        # 切换到新页面
        self.stack.setCurrentIndex(index)
        
        # 初始化新页面
        new_widget = self.stack.widget(index)
        if hasattr(new_widget, 'init_page'):
            new_widget.init_page()

#***************************************************************************************************************
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())